package com.tanhua.manage.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.manage.pojo.OperationLog;
import com.tanhua.manage.service.OperationLogService;
import com.tanhua.manage.util.NoAuthorization;
import com.tanhua.manage.vo.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.stream.Collectors;

@RestController

@RequestMapping("/manage")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;

    @NoAuthorization
    @PostMapping("/queryById")
    public OperationLog queryById(@RequestParam(name = "id", defaultValue = "1") Long id) {
        //OperationLog operationLog =


        return operationLogService.queryById(id);

    }


    /**
     * 消息翻页
     *
     * @param page     当前页码
     * @param pageSize 页尺寸

     * @param sd       开始时间
     * @param ed       结束时间

     * @return 消息分页数据
     */
    @NoAuthorization
    @PostMapping("/operationlog")
    public Object findByPage(@RequestParam(name = "page", defaultValue = "1") Integer page
            , @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize
            , @RequestParam(name = "username", required = false) String username
            , @RequestParam(name = "sd", required = false) Long sd
            , @RequestParam(name = "ed", required = false) Long ed
            , @RequestParam(name = "operation", required = false) String operation
            , @RequestParam(name = "describe", required = false) String describe) {

        if((StrUtil.isNotEmpty(username)&&username.length()>12)||(StrUtil.isNotEmpty(operation)&&operation.length()>12))
        {
            return "你输入的字符长度不能超过12";
        }
        IPage<OperationLog> infoPage = operationLogService.findInfoByPage(page - 1, pageSize, username, sd, ed, operation, describe);


        return new Pager(infoPage.getTotal()
                , infoPage.getSize()
                , infoPage.getPages()
                , infoPage.getCurrent()
                , infoPage.getRecords().stream().map(info -> BeanUtil.toBean(info, OperationLog.class)).collect(Collectors.toList())
                );

    }

}